import VueRouter from 'vue-router';
import Vue from 'vue';
import { Message } from 'element-ui';
import AES from '@/util/crypto';

// 业务区组件
import Home from '@/pages/vocational/Home';
import Booking from '@/pages/vocational/BookingManage';
import EquipmentSearch from '@/pages/vocational/EquipmentSearch';
import EquipmentRepair from '@/pages/vocational/EquipmentRepair';
import ProjectApply from '@/pages/vocational/ProjectApply';
import BookingRecord from '@/pages/vocational/Statistics/BookingRecord';
import ProjectStatistics from '@/pages/vocational/Statistics/ProjectStatistics';
// 登录注册组件
import Login from '@/pages/Login';
import Register from '@/pages/Register';
// 系统设置区组件
import _notice from '@/pages/Setting/_notice';
import _room from '@/pages/Setting/_room';
import _equipment from '@/pages/Setting/_equipment';
import _project from '@/pages/Setting/_project';
import _purview from '@/pages/Setting/_purview';


Vue.use(VueRouter)

let originPush=VueRouter.prototype.push
let originReplace=VueRouter.prototype.replace

//重写push/replace
VueRouter.prototype.push=function(location,resolve,reject){
    if(resolve&&reject){
        originPush.call(this,location,resolve,reject)
    }else{
        originPush.call(this,location,()=>{},()=>{})
    }
}

VueRouter.prototype.replace=function(location,resolve,reject){
    if(resolve&&reject){
        originReplace.call(this,location,resolve,reject)
    }else{
        originReplace.call(this,location,()=>{},()=>{})
    }
}

export default new VueRouter({
    mode:'hash',
    routes:[
        {
            path:'/vocational/Home',
            name:'Home',
            components:{
                work:Home
            },
            meta:{
                isShow:true
            },
            beforeEnter(to,from,next){
                if(sessionStorage.getItem("userObj")===null){
                    next({path:'/Login'})
                    Message({
                        message:'请先登录',
                        type:'info'
                    });
                }else{
                    document.title='主页';
                    next();
                }
            },
        },
        {
            path:'/vocational/Booking',
            name:'Booking',
            components:{
                work:Booking
            },
            meta:{
                isShow:true
            },
            beforeEnter(to,from,next){
                if(sessionStorage.getItem("userObj")===null){
                    next({path:'/Login'})
                    Message({
                        message:'请先登录',
                        type:'info'
                    });
                }else{
                    document.title='预约教室';
                    next();
                }
            }
        },
        {
            path:'/vocational/EquipmentSearch',
            name:'EquipmentSearch',
            components:{
                work:EquipmentSearch
            },
            meta:{
                isShow:true
            },
            beforeEnter(to,from,next){
                if(sessionStorage.getItem("userObj")===null){
                    next({path:'/Login'})
                    Message({
                        message:'请先登录',
                        type:'info'
                    });
                }else{
                    document.title='设备查询';
                    next();
                }
            }
        },
        {
            path:'/vocational/EquipmentRepair',
            name:'EquipmentRepair',
            components:{
                work:EquipmentRepair
            },
            meta:{
                isShow:true
            },
            beforeEnter(to,from,next){
                if(sessionStorage.getItem("userObj")===null){
                    next({path:'/Login'})
                    Message({
                        message:'请先登录',
                        type:'info'
                    });
                }else{
                    document.title='设备报修';
                    next();
                }
            }
        },
        {
            path:'/vocational/ProjectApply',
            name:'ProjectApply',
            components:{
                work:ProjectApply
            },
            meta:{
                isShow:true
            },
            beforeEnter(to,from,next){
                if(sessionStorage.getItem("userObj")===null){
                    next({path:'/Login'})
                    Message({
                        message:'请先登录',
                        type:'info'
                    });
                }else{
                    document.title='项目申请';
                    next();
                }
            }
        },
        {
            path:'/vocational/BookingRecord',
            name:'BookingRecord',
            components:{
                work:BookingRecord
            },
            meta:{
                isShow:true
            },
            beforeEnter(to,from,next){
                if(sessionStorage.getItem("userObj")===null){
                    next({path:'/Login'})
                    Message({
                        message:'请先登录',
                        type:'info'
                    });
                }else{
                    document.title='预约记录';
                    next();
                }
            }
        },
        {
            path:'/vocational/ProjectStatistics',
            name:'ProjectStatistics',
            components:{
                work:ProjectStatistics
            },
            meta:{
                isShow:true
            },
            beforeEnter(to,from,next){
                if(sessionStorage.getItem("userObj")===null){
                    next({path:'/Login'})
                    Message({
                        message:'请先登录',
                        type:'info'
                    });
                }else{
                    document.title='项目统计';
                    next();
                }
            }
        },
        {
            path:'/setting/_noticeManage',
            name:'_notice',
            components:{
                work:_notice
            },
            meta:{
                isShow:true
            },
           // 配置路由前置守卫，当sessionStorage/store中用户权限不足时就不予以跳转，并提示用户权限不足
           beforeEnter(to,from,next){
                if(sessionStorage.getItem("userObj")===null){
                    next({path:'/Login'})
                    Message({
                        message:'请先登录',
                        type:'info'
                    });
                }else{
                    if(JSON.parse(AES.decrypt(sessionStorage.getItem("userObj"))).role==='Root'||JSON.parse(AES.decrypt(sessionStorage.getItem("userObj"))).role==='admin'){
                        document.title='公告管理';
                        next();
                    }else{
                        alert('抱歉您无权限查看')
                    }
                }
            }
        },
        {
            path:'/setting/_roomManage',
            name:'_room',
            components:{
                work:_room
            },
            meta:{
                isShow:true
            },
           // 配置路由前置守卫，当sessionStorage/store中用户权限不足时就不予以跳转，并提示用户权限不足
           beforeEnter(to,from,next){
                if(sessionStorage.getItem("userObj")===null){
                    next({path:'/Login'})
                    Message({
                        message:'请先登录',
                        type:'info'
                    });
                }else{
                    if(JSON.parse(AES.decrypt(sessionStorage.getItem("userObj"))).role==='Root'||JSON.parse(AES.decrypt(sessionStorage.getItem("userObj"))).role==='admin'){
                        document.title='教室管理';
                        next();
                    }else{
                        alert('抱歉您无权限查看')
                    }
                }
            }
        },
        {
            path:'/setting/_equipmentManage',
            name:'_equipment',
            components:{
                work:_equipment
            },
            meta:{
                isShow:true
            },
           // 配置路由前置守卫，当sessionStorage/store中用户权限不足时就不予以跳转，并提示用户权限不足
           beforeEnter(to,from,next){
                if(sessionStorage.getItem("userObj")===null){
                    next({path:'/Login'})
                    Message({
                        message:'请先登录',
                        type:'info'
                    });
                }else{
                    if(JSON.parse(AES.decrypt(sessionStorage.getItem("userObj"))).role==='Root'||JSON.parse(AES.decrypt(sessionStorage.getItem("userObj"))).role==='admin'){
                        document.title='设备管理';
                        next();
                    }else{
                        alert('抱歉您无权限查看')
                    }
                }
            }
        },
        {
            path:'/setting/_projectManage',
            name:'_project',
            components:{
                work:_project
            },
            meta:{
                isShow:true
            },
           // 配置路由前置守卫，当sessionStorage/store中用户权限不足时就不予以跳转，并提示用户权限不足
           beforeEnter(to,from,next){
                if(sessionStorage.getItem("userObj")===null){
                    next({path:'/Login'})
                    Message({
                        message:'请先登录',
                        type:'info'
                    });
                }else{
                    if(JSON.parse(AES.decrypt(sessionStorage.getItem("userObj"))).role==='Root'||JSON.parse(AES.decrypt(sessionStorage.getItem("userObj"))).role==='admin'){
                        document.title='项目管理';
                        next();
                    }else{
                        alert('抱歉您无权限查看')
                    }
                }
            }
        },
        {
            path:'/setting/_purviewManage',
            name:'_purview',
            components:{
                work:_purview
            },
            meta:{
                isShow:true
            },
           // 配置路由前置守卫，当sessionStorage/store中用户权限不足时就不予以跳转，并提示用户权限不足
           beforeEnter(to,from,next){
                if(sessionStorage.getItem("userObj")===null){
                    next({path:'/Login'})
                    Message({
                        message:'请先登录',
                        type:'info'
                    });
                }else{
                    if(JSON.parse(AES.decrypt(sessionStorage.getItem("userObj"))).role==='Root'||JSON.parse(AES.decrypt(sessionStorage.getItem("userObj"))).role==='admin'){
                        document.title='权限管理';
                        next();
                    }else{
                        alert('抱歉您无权限查看')
                    }
                }
            }
        },
        {
            path:'/Login',
            name:'Login',
            components:{
                system:Login
            },
            meta:{
                isShow:false,
            
            },
            beforeEnter(to,from,next){
                document.title='登录';
                next();
            }
        },
        {
            path:'/Register',
            name:'Register',
            components:{
                system:Register
            },
            meta:{
                isShow:false
            },
            beforeEnter(to,from,next){
                document.title='注册';
                next();
            }
        },
        {
            path:'*',
            redirect:'/Login'
            // redirect:'/setting/_roomManage'
        }
    ]
})